﻿namespace EFS.RC.CCAdmin.Offices
{
	public partial class List : EFS.RC.CCAdmin.Common.Control
	{
		#region Constructor

		public List()
		{
			InitializeComponent();

			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listener up for list load
				UtilitiesService.OfficeEndPoints_GetAllCompleted += new System.EventHandler<WebProxy.OfficeEndPoints_GetAllCompletedEventArgs>(UtilitiesService_OfficeEndPoints_GetAllCompleted);

				//listener for delete
				UtilitiesService.OfficeEndPoint_DeleteCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_OfficeEndPoint_DeleteCompleted);
			}

			// set events
			this.Loaded += new System.Windows.RoutedEventHandler(List_Loaded);
		}

		#endregion

		#region Service Events

		void UtilitiesService_OfficeEndPoint_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to delete Office. ", e.Error);
			}
			else
			{
				LoadOffices();
			}
		}

		void UtilitiesService_OfficeEndPoints_GetAllCompleted(object sender, WebProxy.OfficeEndPoints_GetAllCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Failed to retrieve Offices. ", e.Error);
			}

			// check ny
			if (null == e.Result)
			{
				return;
			}

			try
			{
				// deserialize
				EFS.RC.CC.Deployment.Entities.EndPoints.Office[] offices =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Entities.EndPoints.Office[]>(e.Result.ToString());

				//bind/fill form
				dataGridOffices.ItemsSource = offices;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to load data. " + ex.Message);
			}
		}

		#endregion

		#region Load

		private void LoadOffices()
		{
			UtilitiesService.OfficeEndPoints_GetAllAsync();
		}

		#endregion

		#region Events

		private void List_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				LoadOffices();
			}
		}

		private void ToolbarItem_New_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.Common.UI.Controls.Toolbar.ToolbarItem toolbarItem = (EFS.Common.UI.Controls.Toolbar.ToolbarItem)sender;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Offices.Edit(wnd, null);
			wnd.Show(OnNewWindowClosed);
		}

		private void EditButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.RC.CC.Deployment.Entities.EndPoints.Office office = (EFS.RC.CC.Deployment.Entities.EndPoints.Office)dataGridOffices.SelectedItem;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Offices.Edit(wnd, office);
			wnd.Show(OnEditWindowClosed);
		}

		private void DeleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete office?"))
			{
				EFS.RC.CC.Deployment.Entities.EndPoints.Office office = (EFS.RC.CC.Deployment.Entities.EndPoints.Office)dataGridOffices.SelectedItem;
				UtilitiesService.OfficeEndPoint_DeleteAsync(office.ID.Value);
			}
		}

		#endregion

		#region Edit windows closed 

		private void OnNewWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				// load
				LoadOffices();
			}
		}

		//update office
		private void OnEditWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				// load
				LoadOffices();
			}
		}

		#endregion

	}
}
